package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.LabelInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Stream;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    LeaseTermMapper leaseTermMapper;
    @Autowired
    LeaseAgreementService leaseAgreementService;
    @Autowired
    BrowsingHistoryService browsingHistoryService;




    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfoMapper.selectById(id), roomDetailVo);

//        @Schema(description = "所属公寓信息")
//        private ApartmentItemVo apartmentItemVo;
        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.selectByRoomId(roomDetailVo.getApartmentId());
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
//
//        @Schema(description = "图片列表")
//        private List<GraphVo> graphVoList;
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = graphInfos.stream().map(graphInfo -> new GraphVo(graphInfo.getName(), graphInfo.getUrl())).toList();
        roomDetailVo.setGraphVoList(graphVoList);
//
//        @Schema(description = "属性信息列表")
//        private List<AttrValueVo> attrValueVoList;
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
//
//        @Schema(description = "配套信息列表")
//        private List<FacilityInfo> facilityInfoList;
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id);
        List<RoomFacility> roomFacilities = roomFacilityMapper.selectList(roomFacilityLambdaQueryWrapper);
        List<FacilityInfo> facilityInfoList = roomFacilities.stream().map(roomFacility -> facilityInfoMapper.selectById(roomFacility.getFacilityId())).toList();
        roomDetailVo.setFacilityInfoList(facilityInfoList);

//
//        @Schema(description = "标签信息列表")
//        private List<LabelInfo> labelInfoList;
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id);
        List<RoomLabel> roomLabels = roomLabelMapper.selectList(roomLabelLambdaQueryWrapper);
        List<LabelInfo> labelInfoList = roomLabels.stream().map(roomLabel -> labelInfoMapper.selectById(roomLabel.getLabelId())).toList();
        roomDetailVo.setLabelInfoList(labelInfoList);

//
//        @Schema(description = "支付方式列表")
//        private List<PaymentType> paymentTypeList;
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id);
        List<RoomPaymentType> roomPaymentTypes = roomPaymentTypeMapper.selectList(roomPaymentTypeLambdaQueryWrapper);
        List<PaymentType> paymentTypeList = roomPaymentTypes.stream().map(roomPaymentType -> paymentTypeMapper.selectById(roomPaymentType.getPaymentTypeId())).toList();
        roomDetailVo.setPaymentTypeList(paymentTypeList);
//
//        @Schema(description = "杂费列表")
//        private List<FeeValueVo> feeValueVoList;
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoListByApartmentId(roomDetailVo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeValueVoList);
//
//        @Schema(description = "租期列表")
//        private List<LeaseTerm> leaseTermList;
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id);
        List<RoomLeaseTerm> roomLeaseTerms = roomLeaseTermMapper.selectList(roomLeaseTermLambdaQueryWrapper);
        List<LeaseTerm> leaseTermList = roomLeaseTerms.stream().map(roomLeaseTerm -> leaseTermMapper.selectById(roomLeaseTerm.getLeaseTermId())).toList();
        roomDetailVo.setLeaseTermList(leaseTermList);
//
//        @Schema(description = "房间是否被删除")
//        private Boolean isDelete;
        roomDetailVo.setIsDelete(roomDetailVo.getIsDeleted()==1);
//
//        @Schema(description = "房间是否已入住")
//        private Boolean isCheckIn;
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);

        roomDetailVo.setIsCheckIn(leaseAgreementService.count(leaseAgreementLambdaQueryWrapper) > 0);

        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);


        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        return roomInfoMapper.pageItemByApartmentId(roomItemVoPage,id);
    }
}




